import wanlshop_config from '@/common/config/config';

const func = {
	upImage(func) {
		uni.chooseImage({
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				console.log('filepath', tempFilePaths[0])
				this.upload(tempFilePaths[0], func);
			},
			fail: err => {
				console.log('-=-=-=errimg', err)
			}
		});
	},
	upload(filePath, func) {
		uni.uploadFile({
			url: wanlshop_config.appurl + '/common/upload', //仅为示例，非真实的接口地址
			filePath: filePath,
			name: "file",
			success: (uploadFileRes) => {
				let data = JSON.parse(uploadFileRes.data)
				func instanceof Function && func(data)
				// console.log(JSON.parse(uploadFileRes.data));
				// let url = JSON.parse(uploadFileRes.data).data.url;
				// url = this.getImgUrl(url);
				// self.form.shop_img.push(url);
			},
			complete: () => {
				// uni.hideLoading()
			}
		});
	},
	isMobile: function(mobile) {
		return /^1[3-9]\d{9}$/.test(mobile)
	},
	bigImage(list, current) {
		wx.previewImage({
			urls: list, //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
			current: '', // 当前显示图片的http链接，默认是第一个
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
	copy(text) {
		uni.setClipboardData({
			data: text,
			showToast: true,
			success: res => {
				console.log('-=-=-=copy111', res)
			},
			fail: err => {
				console.log('-=-=-=copy222', err)
			}
		})
	},
	getLongIndex(...args) {
		let longIndex = 0
		for (let i of args) {
			let str = i.toString()
			let index = str.indexOf('.')
			if (index !== -1) {
				let long = str.length - index - 1
				if (long > longIndex) {
					longIndex = long
				}
			}
		}
		return Math.pow(10, longIndex)
	},
	toNumber(...args) {
		return args.map(v => v * 1)
	},
	bcadd(...args) {
		args = this.toNumber(...args)
		let long = this.getLongIndex(...args)
		let num = 0
		for (let i of args) {
			num += (i * long)
		}
		return num / long
	},
	bcsub(...args) {
		args = this.toNumber(...args)
		let long = this.getLongIndex(...args)
		let num = args[0] * long
		for (let i = 0; i < args.length; i++) {
			if (i > 0) {
				num -= (args[i] * long)
			}
		}
		return num / long
	},
	setNewVar(list = [], data = {}, childField = 'children') {
		return list.map(v => {
			let item = {
				...v,
				...data
			}
			if (v[childField]) {
				item[childField] = this.setNewVar(v[childField], data, childField)
			}
			return item
		})
	}
}

export default func